package com.lottery.im.util;

import android.os.CountDownTimer;
import android.widget.TextView;
import com.lottery.im.R;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.util.rxbus.RxBus;

/**
 * Created by ztz on 2017/9/9.
 */

public class TimerUtils extends CountDownTimer {
  private TextView textView;

  public TimerUtils(TextView v, long millisInFuture, long countDownInterval) {
    //微调防止跳过
    super(millisInFuture + millisInFuture * 2 / countDownInterval, countDownInterval);
    textView = v;
  }

  @Override
  public void onTick(long millisUntilFinished) {
    if (textView == null) return;
    textView.setText(formatDuring2(millisUntilFinished));
  }

  @Override
  public void onFinish() {
    cancel();
    textView.setText(R.string.opening);
    RxBus.get().send(RxBusCode.COUNT_DOWN_CODE);
  }

  /**
   * @param mss 要转换的毫秒数
   * @return 该毫秒数转换为 * 时 * 分 * 秒 后的格式
   * @author tz.zhang
   */
  public static String formatDuring(long mss) {
    long days = mss / (1000 * 60 * 60 * 24);
    long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
    long seconds = (mss % (1000 * 60)) / 1000;
    if (days > 0) {
      return days + "天" + hours + "时" + minutes + "分" + seconds + "秒";
    } else {
      if (hours > 0) {
        return hours + "时" + minutes + "分" + seconds + "秒";
      } else {
        if (minutes > 0) {
          return minutes + "分" + seconds + "秒";
        } else {
          return seconds + "秒";
        }
      }
    }
  }

  /**
   * @param mss 要转换的毫秒数
   * @return 该毫秒数转换为 00:00:00 后的格式
   * @author tz.zhang
   */
  public static String formatDuring2(long mss) {
    long hours = mss / (1000 * 60 * 60);
    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
    long seconds = (mss % (1000 * 60)) / 1000;
    String h = hours < 10 ? ("0" + hours) : ("" + hours);
    String m = minutes < 10 ? ("0" + minutes) : ("" + minutes);
    String s = seconds < 10 ? ("0" + seconds) : ("" + seconds);
    return h + ":" + m + ":" + s;
  }

  public static boolean isCountDown(long mss) {
    long days = mss / (1000 * 60 * 60 * 24);
    long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
    long seconds = (mss % (1000 * 60)) / 1000;
    if (days > 0) {
      return false;
    } else {
      if (hours > 0) {
        return false;
      } else {
        if (minutes == 1 && seconds == 0) {
          return true;
        } else {
          return false;
        }
      }
    }
  }

  public static boolean isTwoMin(long mss) {
    long days = mss / (1000 * 60 * 60 * 24);
    long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
    if (days > 0) {
      return false;
    } else {
      if (hours > 0) {
        return false;
      } else {
        if (minutes >= 2) {
          return true;
        } else {
          return false;
        }
      }
    }
  }

  public static boolean isMinutes(long mss) {
    long seconds = (mss % (1000 * 60)) / 1000;
    return seconds % 10 == 0;
    //        if (seconds == 30) {
    //            return true;
    //        } else return seconds == 0;
  }
}
